<template>
  <div class="player" :class="[autoClass]" :style="{ 'background-color': background }">
    <video-player class="vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="playerOptions" @canplay="ready"
      @ended="onPlayerEnded($event)" @timeupdate="onPlayerTimeupdate($event)">
    </video-player>
  </div>
</template>
<script>
// import Vue from "vue";
import VideoPlayer from 'vue-video-player'
import 'vue-video-player/src/custom-theme.css'
import '../../static/style/video-js.css'
Vue.use(VideoPlayer)
export default {
  props: {
    source: {
      type: Object,
      default: () => { return {} },
    },
    autoplay: {
      type: Boolean,
      default: true,
    },
    loop: {
      type: Boolean,
      default: false,
    },
    poster: {
      type: String,
      default: "",
    },
    background: {
      type: String,
      default: "#191919",
    },
    isPlay: {
      type: Boolean,
      default: false,
    }
  },
  components: {
  },
  data() {
    return {
      time: 0,
      playerOptions: {
        //播放速度
        playbackRates: [0.5, 1.0, 1.5, 2.0],
        //如果true,浏览器准备好时开始回放。
        autoplay: this.autoplay,
        // 默认情况下将会消除任何音频。
        muted: false,
        // 导致视频一结束就重新开始。
        loop: this.loop,
        // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        preload: true,
        language: 'zh-CN',
        // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        aspectRatio: '16:9',
        // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        fluid: true,
        sources: [{
          // type: "video/mp4", //类型
          src: this.source.url,//url地址
        }],
        //你的封面地址
        poster: this.poster || "",
        //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        notSupportedMessage: '此视频暂无法播放，请稍后再试',
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true
        }
      },
      autoClass: "",
    }
  },
  mounted() {
    if (this.isPlay) {
      this.play();
    }
  },
  watch: {
    isPlay(newVal) {
      if (newVal) {
        this.play();
      } else {
        this.pause();
      }
    }
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player
    }
  },
  methods: {
    onPlayerTimeupdate(player) {
      let time = player.cache_.currentTime;
      if (time > this.time) {
        this.time = time;
      }
    },
    onPlayerEnded(player) {

    },
    ready(player) {
      this.autoClass = "auto";
    },
    play() {
      var userAgent = navigator.userAgent.toLowerCase();
      // 判断是否在微信中打开
      if (userAgent.indexOf('micromessenger') !== -1) {
        WeixinJSBridge.invoke(
          "weiixinJSBridgeReady",
          {},
          () => { this.player.play() },
        )
      } else {
        this.player.play();
      }
    },
    pause() {
      this.player.pause();
    },
    playerClick() {
    }
  },
  beforeDestroy() {

  },
}
</script>
<style lang="scss" >
.player {
  width: 100%;
  height: 100%;
  text-align: center;
  border-radius: 4px;
  // &::after{
  //   content: "";
  //   position: absolute;
  //   left: 0;
  //   top: 0;
  //   bottom: 0;
  //   right: 0;
  // }
}

.player.auto {
  height: 100%;
}

.vjs-custom-skin>.video-js {
  padding-top: 0 !important;
  height: 100% !important;
}

.video-js .vjs-tech {
  position: relative !important;
}

.player .video-js {
  background-color: transparent;
}

.player .video-player {
  width: 100%;
  height: 100%;
}

.player .vjs-error .vjs-error-display .vjs-modal-dialog-content {
  font-size: 0.28rem;
}

.player .vjs-custom-skin>.video-js .vjs-big-play-button {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: unset !important;
  height: 50px !important;
  width: 50px !important;
  line-height: 50px !important;
  border-radius: 50%;
  font-size: 26px !important;
  background-color: rgba(0, 0, 0, 0.45) !important;
}

@media screen and (max-width: 750px) {
  .player .vjs-custom-skin>.video-js .vjs-big-play-button {
    font-size: 0.48rem !important;
    height: 0.9rem !important;
    width: 0.9rem !important;
    line-height: 0.9rem !important;
    margin-top: 0 !important;
  }
}
</style>